package com.welike.shibo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.welike.shibo.eneity.Goods;
import com.welike.shibo.service.ShopHttpService;
import com.welike.shibo.utils.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

/**
 * 封装建德商城接口对接
 * @author fangyu
 * @version v1.0.0
 * @since 2019-09-10 13:36
 */
@Slf4j
@Service
public class ShopHttpServiceImpl implements ShopHttpService {


    /**
     * 通过id查询商品名
     * @param id 商品id
     * @return 商品名
     */
    @Override
    public Goods getGoodsInfoById(String id) {
        Goods goods = null;
        String base_url = "https://www.healthplatform.xyz/api/v2/common/goods/" + id;
        HttpHeaders headers = new HttpHeaders();
        String response = HttpClient.sendGetRequest(base_url, null, headers);
        JSONObject jsonObject = JSON.parseObject(response);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data != null){
            goods = new Goods();
            goods.setShopId(id);
            goods.setName(data.get("goods_name").toString());
            goods.setImg(data.get("goods_image").toString());
        }
        return goods;
    }

    /**
     * 通过手机号查询会员等级
     * @param phone 手机号
     * @return 会员等级
     */
    @Override
    public Integer getUserLevelByPhone(String phone) {
        String base_url = "https://www.healthplatform.xyz/api/v2/common/member/" + phone;
        HttpHeaders headers = new HttpHeaders();
        String response = HttpClient.sendGetRequest(base_url, null, headers);
        JSONObject jsonObject = JSON.parseObject(response);
        JSONObject data = jsonObject.getJSONObject("data");
        Integer vip_level = 0;  // 设置默认等级为0，普通会员
        if (data != null){
            vip_level = Integer.valueOf(data.get("vip_level").toString());
        }
        log.info("vip_level ==> {}", vip_level);
        return vip_level;
    }
}
